import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.running;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;

import models.Cart;
import models.LoanInFunding;

import org.junit.Test;

import prefix.duydo.repository.JpaRepository;
import prefix.duydo.repository.Repository;
import prefix.duydo.repository.spec.Specification;
import prefix.duydo.repository.spec.SpecificationBuilder;
import prefix.platform.Global;
import repository.CartRepository;

public class JPALab {

    public void jpaBuilder() {

        List<Cart> list = null;
        try {
            list = CartRepository.getListByProperties(Cart.class, new String[] { "status", "aid" }, new Object[] { 0,
                    10006 });
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.print(list.size());
    }

    @Test
    public void emFactory() {
        running(fakeApplication(), new Runnable() {
            @Override
            public void run() {
                EntityManager em = Global.factory.createEntityManager();
                Cart cart = em.find(Cart.class, 10016);
                System.out.print(cart.getAmount());
            }
        });

    }

    public void jpaRepository() {
        Map<EntityManager, LoanInFunding> emMap = new HashMap<EntityManager, LoanInFunding>();
        for (int i = 0; i < 30; i++) {
            EntityManager em = Global.factory.createEntityManager();
            Repository repo = new JpaRepository(em);
            SpecificationBuilder builder = SpecificationBuilder.forProperty("loanId");
            Specification<LoanInFunding> isLoan = builder.equal(10014).build();
            LoanInFunding loan = repo.find(LoanInFunding.class, isLoan).single();
            System.out.println(i);
            System.out.println(em);
            System.out.println(em.getMetamodel());
            System.out.println(em.contains(loan));
            System.out.println(loan.getAppAmount());
            em.close();
            System.out.println(em.contains(loan));
            emMap.put(em, loan);
        }
        int j = 0;
        for (EntityManager em : emMap.keySet()) {
            System.out.println(j++);
            System.out.println(em);
            System.out.println(em.getMetamodel());
            System.out.println(em.contains(emMap.get(em)));
            System.out.println(emMap.get(em).getAppAmount());
            em.close();
            System.out.println(em);
            System.out.println(em.getMetamodel());
            System.out.println(em.contains(emMap.get(em)));
            System.out.println(emMap.get(em).getAppAmount());
        }

    }
}
